import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;




public class GetDomains {

	public GetDomains()
	{
		
	}
	public static void main(String args[]) throws IOException{
		URL url = new URL("http://192.168.1.13:9888/getdomain");
		HttpURLConnection con = (HttpURLConnection) url.openConnection();
		con.setRequestMethod("GET");
		con.setRequestProperty("Connection", "keep-alive");
		con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0) Gecko/20100101 Firefox/10.0");
		String charset="UTF-8";
		StringBuilder sb = new StringBuilder();
		BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));
		BufferedWriter out = new BufferedWriter(new FileWriter("C:\\Users\\PC\\Downloads\\domains.txt"));
		while(true){
			String line = br.readLine();
			if(line == null)
				break;
			sb.append(line);
		}
		String json = sb.toString();
		Gson gson= new Gson();
		ArrayList<String> domains = gson.fromJson(json,new TypeToken<ArrayList<String>>(){}.getType());
		Object[] strDomains = domains.toArray();
		
		Arrays.sort(strDomains);
		for(int i = 0;i<strDomains.length;++i)
		{
			String domain = (String) strDomains[i];
			//System.out.println(domain);
			out.write(domain+"\n");
		}
		out.flush();
		out.close();
		  
		GetDomains.sortDomains("C:\\Users\\PC\\Downloads\\domain_white_list.txt", "C:\\Users\\PC\\Downloads\\domain_white_list.sorted");
		
		
		
	}
	
	
	public static void sortDomains(String infile,String outfile) throws IOException
	{
		BufferedReader in = new BufferedReader(new FileReader(infile));
		BufferedWriter out = new BufferedWriter(new FileWriter(outfile));
		String line;
		ArrayList<String> domains = new ArrayList<String>();
		String outStr="";
		while((line = in.readLine())!=null)
		{
			domains.add(line);
		}
		Object[] strDomains = domains.toArray();
		Arrays.sort(strDomains);
		for(int i = 0;i<strDomains.length;++i)
		{
			String domain = (String) strDomains[i];
			//System.out.println(domain);
			outStr+=domain+"\n";
		}
		out.write(outStr);
		out.flush();
		out.close();
		
	}
}
